# -*- coding: utf-8 -*-            
# @Time : 2022/10/29 23:34
# @Author  : lining
# @FileName: demo6.py
"""
根据 逆波兰表示法，求表达式的值。

有效的算符包括 +、-、*、/ 。每个运算对象可以是整数，也可以是另一个逆波兰表达式。

注意 两个整数之间的除法只保留整数部分。

可以保证给定的逆波兰表达式总是有效的。换句话说，表达式总会得出有效数值且不存在除数为 0 的情况。

运算符在右边，遇到数字就入栈，遇到运算符就弹出栈顶两个数字
tokens = ["2","1","+","3","*"]
tokens = ["4","13","5","/","+"]
tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
用栈存数字
"""
class Solution(object):
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        # 存放数据栈
        stack = []
        for i in tokens:
            # 如果是数字就如栈
            if i not in ['+', '-', '*', '/']:
                stack.append(i)
            # 不是数字取出栈顶两个数字，运算后入栈
            else:
                a = stack.pop()
                b = stack.pop()
                c = eval(f'{b}{i}{a}')
                stack.append(c)
        print(stack)


tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Solution().evalRPN(tokens)
